package com.appinterfacelite.service.impl;

import com.appinterfacelite.entity.Home_userEntity;
import com.appinterfacelite.entity.Homepage_detailsEntity;
import com.appinterfacelite.entity.RoleEntity;
import com.appinterfacelite.service.MenuService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据登陆人id查询菜单
 * 返回json字符串
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    Home_userServiceImpl home_userService;
    @Autowired
    Homepage_detailsServiceImpl homepage_detailsService;
    @Autowired
    RoleServiceImpl roleService;


    @Override
    public JSONObject queryMenu(String user_id) {
        Map<String, Object> map = new HashMap<>();
        List<Map> mapList = new ArrayList<>();
        List<Home_userEntity> home_userEntity = home_userService.getBaseMapper().getByUserId(user_id);
        if (!home_userEntity.isEmpty()) {//能在home_user中查询该用户
            List<Homepage_detailsEntity> homepage_detailsEntityList = homepage_detailsService.getBaseMapper().homepage_detail_select(user_id);
            if (!homepage_detailsEntityList.isEmpty()) {//如果可以根据user_id查询出菜单
                for (int i = 0; i < homepage_detailsEntityList.size(); i++) {
                    //防呆机制:若有空行，则跳过此轮循环
                    if(homepage_detailsEntityList.get(i)==null)
                        continue;
                    Map<String, Object> homepage_detailsMap = new HashMap<>();
                    homepage_detailsMap.put("title_wgj", homepage_detailsEntityList.get(i).getHomepage_title() + "");
                    homepage_detailsMap.put("name1", homepage_detailsEntityList.get(i).getOne_menu() + "");
                    homepage_detailsMap.put("name2", homepage_detailsEntityList.get(i).getTwo_menu() + "");
                    homepage_detailsMap.put("nPath", homepage_detailsEntityList.get(i).getUrl() + "");
                    homepage_detailsMap.put("nodeId", homepage_detailsEntityList.get(i).getHomepage_id() + "");
                    mapList.add(homepage_detailsMap);
                }
                map.put("code", 200);
                map.put("message", "success");
                map.put("data", mapList);
                JSONObject jsonObject = JSONObject.fromObject(map);
                return jsonObject;
            } else {
                map.put("code", 204);
                map.put("message", "no menu_id searched!");
                JSONObject jsonObject = JSONObject.fromObject(map);
                return jsonObject;
            }

        } else {//不能在home_user中查询该用户
            RoleEntity roleEntity = roleService.getBaseMapper().role_id_select(user_id);
            if (roleEntity != null) {//能在role表查询信息
                List<Homepage_detailsEntity> homepage_detailsEntityList = homepage_detailsService.getBaseMapper().menu_select(roleEntity.getRole_id() + "");
                if (!homepage_detailsEntityList.isEmpty()) {//可以根据role_id查新菜单
                    for (int i = 0; i < homepage_detailsEntityList.size(); i++) {
                        //防呆机制:若有空行，则跳过此轮循环
                        if(homepage_detailsEntityList.get(i)==null)
                            continue;
                        Map<String, Object> homepage_detailsMap = new HashMap<>();
                        homepage_detailsMap.put("title_wgj", homepage_detailsEntityList.get(i).getHomepage_title() + "");
                        homepage_detailsMap.put("name1", homepage_detailsEntityList.get(i).getOne_menu() + "");
                        homepage_detailsMap.put("name2", homepage_detailsEntityList.get(i).getTwo_menu() + "");
                        homepage_detailsMap.put("nPath", homepage_detailsEntityList.get(i).getUrl() + "");
                        homepage_detailsMap.put("nodeId", homepage_detailsEntityList.get(i).getHomepage_id() + "");
                        mapList.add(homepage_detailsMap);
                    }
                    map.put("code", 200);
                    map.put("message", "success");
                    map.put("data", mapList);
                    JSONObject jsonObject = JSONObject.fromObject(map);
                    return jsonObject;
                } else {//不能根据role_id查询菜单
                    map.put("code", 204);
                    map.put("message", "no menu_id searched!");
                    JSONObject jsonObject = JSONObject.fromObject(map);
                    return jsonObject;
                }
            } else {//不能在role表查询信息
                map.put("code", 204);
                map.put("message", "can not search from table: role!");
                JSONObject jsonObject = JSONObject.fromObject(map);
                return jsonObject;
            }
        }
    }
}
